package com.wisewin.pose.api.controller;

import com.wisewin.pose.api.controller.base.BaseController;
import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.entity.bo.QueryParamBO;
import com.wisewin.pose.entity.db.OrgUserDO;
import com.wisewin.pose.entity.db.UserDO;
import com.wisewin.pose.service.OrgUserService;
import com.wisewin.pose.service.UserPhysicalRecordService;
import com.wisewin.pose.service.UserService;
import com.wisewin.pose.util.MD5Util;
import com.wisewin.pose.util.PageUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/orgUser")
public class OrgUserController extends BaseController {

    @Autowired
    private OrgUserService orgUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserPhysicalRecordService userPhysicalRecordService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public ResultDTO login(@RequestBody OrgUserDO orgUserDO) {
        /* 验证参数是否正确 */
        verification(orgUserDO.getUserName(), orgUserDO.getPasscode());
        // 查找用户信息，密码加密
        OrgUserDO loginUser = orgUserService.queryOrgUserForLogin(orgUserDO.getUserName(),
                MD5Util.digest(orgUserDO.getPasscode()));
        // 用户名或密码不对
        if (loginUser == null) {
            return ResultDTOBuilder.failure("1", "用户名或密码输入有误", null);
        }
        // 账户冻结
        if (loginUser.getIsDel() != 0) {
            return ResultDTOBuilder.failure("1", "当前账号已禁用或者已删除", null);
        }
        String sessionID = super.setOrgLoginUser(loginUser);
        return ResultDTOBuilder.success(sessionID);
    }

    /**
     * 查看登录信息
     */
    @PostMapping("/queryLoginUser")
    public ResultDTO queryLoginUser(HttpServletRequest request) {
        OrgUserDO orgLoginUser = super.getOrgLoginUser(request);
        if (orgLoginUser == null) {
            return ResultDTOBuilder.failure("1", "当前账号未登录或登录超时，请重新登录！", null);
        }
        // 查询用户信息
        OrgUserDO orgUserDO = orgUserService.queryById(orgLoginUser.getId());
        return ResultDTOBuilder.success(orgUserDO);
    }

    /**
     * 查看登录信息
     */
    @PostMapping("/queryById")
    public ResultDTO queryById(@RequestBody OrgUserDO orgUser) {
        if (orgUser.getId() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 查询用户信息
        OrgUserDO orgUserDO = orgUserService.queryById(orgUser.getId());
        if (orgUserDO == null || orgUserDO.getIsDel() != 0) {
            return ResultDTOBuilder.failure("1", "当前账号有误", null);
        }
        return ResultDTOBuilder.success(orgUserDO);
    }

    /**
     * 查询用户列表接口
     */
    @PostMapping("/queryUserPage")
    public ResultDTO queryUserPage(HttpServletRequest request, @RequestBody QueryParamBO query) {
        if (query.getPageNo() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        OrgUserDO orgLoginUser = super.getOrgLoginUser(request);
        if (orgLoginUser == null) {
            return ResultDTOBuilder.failure("1", "当前账号未登录或登录超时，请重新登录！", null);
        }
        // 处理机构ID
        query.setOrgId(orgLoginUser.getId());
        Long pageSize = (query.getPageSize() == null || query.getPageSize() <= 0) ? 10 : query.getPageSize();
        return orgUserService.selectAuthoredUsersByOrgId(query, getPageQuery(query.getPageNo(), pageSize));
    }

    @PostMapping("/getOrgUserPhysicalRecords")
    public ResultDTO getOrgPhysicalRecord(HttpServletRequest request, @RequestBody QueryParamBO query) {
        if (query.getUserId() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        OrgUserDO orgLoginUser = super.getOrgLoginUser(request);
        if (orgLoginUser == null) {
            return ResultDTOBuilder.failure("1", "当前账号未登录或登录超时，请重新登录！", null);
        }
        // 处理机构ID
        query.setOrgId(orgLoginUser.getId());

        if (query.getPageNo() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        Long pageSize = (query.getPageSize() == null || query.getPageSize() <= 0) ? 10 : query.getPageSize();
        PageUtil util = getPageQuery(query.getPageNo(), pageSize);
        return userPhysicalRecordService.queryOrgUserPhysicalRecords(query, util);
    }

    /**
     * 查询用户列表接口 支持按名字模糊匹配
     */
    @PostMapping("/queryRecordPage")
    public ResultDTO queryOrgRecordPage(HttpServletRequest request, @RequestBody QueryParamBO query) {
        if (query.getPageNo() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        OrgUserDO orgLoginUser = super.getOrgLoginUser(request);
        if (orgLoginUser == null) {
            return ResultDTOBuilder.failure("1", "当前账号未登录或登录超时，请重新登录！", null);
        }
        query.setOrgId(orgLoginUser.getId());

        Long pageSize = (query.getPageSize() == null || query.getPageSize() <= 0) ? 10 : query.getPageSize();
        PageUtil util = getPageQuery(query.getPageNo(), pageSize);
        return userPhysicalRecordService.wildcardSearchPhysicalRecordsByOrgId(query, util);
    }

    @PostMapping("/authorOrg")
    public ResultDTO inserAuthor(HttpServletRequest request, @RequestBody QueryParamBO query) {

        Integer row = orgUserService.insertOrIgnore(query.getOpenid(), query.getOrgId());
        return ResultDTOBuilder.success(row);
    }

}
